package com.mach.platform.sprider.dev.virdev;

import com.mach.platform.sprider.getter.CmdMainWork;
import com.mach.platform.sprider.getter.DevFactory;
import com.mach.platform.sprider.getter.GeneralDev;
import com.mach.platform.sprider.getter.GeneralModbusDev;
import com.mach.platform.sprider.getter.DataLogService;
import com.mach.platform.util.SqlHelper;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@Slf4j
public class KongYaPressKeeperAutoCtrl extends GeneralModbusDev implements VirDev {

    private Map<Object,Object> devPropsMap;


    private SqlHelper sqlHelper;
    private  DataLogService dataLogService;
    private  Map<String, Object> devInfoMap;
    private boolean isOpen = false;
    private GeneralDev[] tas;
//    private  Integer devId;


    public KongYaPressKeeperAutoCtrl(Map<String, Object> devInfoMap, SqlHelper sqlHelper, DataLogService dataLogService) throws SQLException {
        super(devInfoMap, sqlHelper, dataLogService);

        this.devInfoMap = devInfoMap;

        this.sqlHelper = sqlHelper;

        this.dataLogService = dataLogService;

        this.devPropsMap = sqlHelper.getMapFrTable("name", "value",
                "select name, value value " +
                        " from iot_dev_prop prop " +
                        " where prop.dev_id=" + this.devId);



        initCtrInfo();
    }

    private void initCtrInfo(){

        this.tas = new GeneralDev[7];

        this.tas[0] = DevFactory.getById(142); //1#空压机
        this.tas[1] = DevFactory.getById(143); //2#空压机
        this.tas[2] = DevFactory.getById(144); //3#空压机
        this.tas[3] = DevFactory.getById(145); //4#空压机
        this.tas[4] = DevFactory.getById(146); //3#空压机
        this.tas[5] = DevFactory.getById(147); //1#空压机
        this.tas[6] = DevFactory.getById(148); //2#空压机

    }

    @Override
    protected void pick() {

        Double upValue = Double.parseDouble((String)this.devPropsMap.get("setUp"));
        Double downValue = Double.parseDouble((String)this.devPropsMap.get("setDown"));

        GeneralDev pressDev = DevFactory.getById(102); //压缩空气总管压力传感器


        if(!this.isOpen)return;

        try {
            Double press = (Double) pressDev.getValueByCollName("压力");

            if(press < downValue){ //超下限

                GeneralDev ta = this.findMostLongKongYa();
                if(ta != null){
                    CmdMainWork.commitCmd(ta.getDevId(),"open","");
                }
            }
            if(press > upValue){ //超上限
                GeneralDev ta = this.findMostLongKongYa();
                if(ta != null){
                    CmdMainWork.commitCmd(ta.getDevId(),"close","");
                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }

    private GeneralDev findMostLongKongYa() {
        GeneralDev tr = null;
        Integer maxRunTime = null;
        for( GeneralDev dev : this.tas ){
            //给定频率
            try {

                if(maxRunTime == null){
                    maxRunTime = (Integer)dev.getValueByCollName("运行时间");
                }
                Integer trunTime = (Integer)dev.getValueByCollName("运行时间");
                if(trunTime > maxRunTime ){
                    maxRunTime = trunTime;
                    tr = dev;
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tr;
    }
    private GeneralDev findMostShortKongYa() {
        GeneralDev tr = null;
        Integer maxRunTime = null;
        for( GeneralDev dev : this.tas ){
            //给定频率
            try {

                if(maxRunTime == null){
                    maxRunTime = (Integer)dev.getValueByCollName("运行时间");
                }
                Integer trunTime = (Integer)dev.getValueByCollName("运行时间");
                if(trunTime < maxRunTime ){
                    maxRunTime = trunTime;
                    tr = dev;
                }


            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return tr;
    }

    @Override
    public void cmd(Integer cmdLogId,String cmd, String param) {

        try {
            Integer devTypeId = (Integer)this.devInfoMap.get("devTypeId");

//            Map<Object, Object> addrMap = sqlHelper.getMapFrTable("name", "valueAddr",
//                    "select name, value_addr valueAddr " +
//                            " from iot_coll_items colls " +
//                            " where colls.dev_type_id=" + devTypeId);

//            if(ctrModelValue != null && ctrModelValue.equals(1.0)){//远程
            if("open".equals(cmd) ){
                this.isOpen = true;
            }
            if("close".equals(cmd) ){
                this.isOpen = false;
            }
            if("setUp".equals(cmd) && param != null ){
                sqlHelper.update("replace into iot_dev_prop(name,value,dev_id)" +
                        "value('setUp','"+param+"',"+devId+")");
            }
            if("setDown".equals(cmd) && param != null ){
                sqlHelper.update("replace into iot_dev_prop(name,value,dev_id)" +
                        "value('setDown','"+param+"',"+devId+")");
            }

            sqlHelper.update(
                    "update iot_dev_cmd_log set resp_code=1,resp_ctx='"+EXE_SUCS_CTX+"' where id=" + cmdLogId);

        } catch (Exception e) {
            e.printStackTrace();
            sqlHelper.updateAsync(
                    "update iot_dev_cmd_log set resp_code=0,resp_ctx='"+e.getLocalizedMessage()+"' where id=" + cmdLogId);
        }finally {
            sqlHelper.updateAsync(
                    "update iot_dev_cmd_log set resp_time=now() where id=" + cmdLogId);
        }




        return;
    }

    @Override
    public Integer getDevId() {
        return this.devId;
    }


}
